import { createRouter, createWebHashHistory } from 'vue-router';

const router = createRouter({
    history: createWebHashHistory(),
    routes: [
        {
            path: '/',
            redirect: '/login',
        },

        {
            path: '/login',
            component: () => import('@/views/user/login'),
            meta: {
                requiresAuth: false,
                title: '登录',
            },
        },

        {
            path: '/home',
            component: () => import('@/views/main/index'),
            meta: {
                requiresAuth: true,
                title: '菜单',
            },
        },

        {
            path: '/404',
            name: 'not-found',
            component: () => import('@/views/result/error-404'),
            meta: {
                requiresAuth: false,
                title: '页面未找到',
            },
        },
        {
            path: '/:pathMatch(.*)*',
            redirect: '/404',
        },
    ],
});

router.beforeEach((to) => {
    // eslint-disable-line
    const requiresAuth = to.meta.requiresAuth;
    if (requiresAuth) {
        // return {
        //   name: 'Error404'
        // }
        return true;
    }
    return true;
});

router.afterEach((to) => {
    const title = to.meta.title;
    document.title = title;
});

export default router;
